﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace ToDoIt2.UI {
	/// <summary>
	/// Interaction logic for ItemTreeViewItem.xaml
	/// </summary>
	public partial class ItemTreeViewItem : TreeViewItem {

//		public ItemViewModel Item { get { return this.Header as ItemViewModel; } }

		public enum DropStates { Accept, Deny, None };

		public static DependencyProperty DropStateProperty = DependencyProperty.RegisterAttached("DropState", typeof(DropStates),
																	typeof(ItemTreeViewItem), new PropertyMetadata(DropStates.None));
		public static DropStates GetDropState(DependencyObject target) { return (DropStates)target.GetValue(DropStateProperty); }
		public static void SetDropState(DependencyObject target, DropStates value) { target.SetValue(DropStateProperty, value); }
		public DropStates DropState {
			get { return (DropStates)this.GetValue(DropStateProperty); }
			set { this.SetValue(DropStateProperty, value); }
		}

		Point LeftMouseDownAt = new Point();

		public ItemTreeViewItem() {
			InitializeComponent();
		}

		protected override DependencyObject GetContainerForItemOverride() {
			return new ItemTreeViewItem();
		}

		private void OnPreviewLeftMouseDown(object sender, MouseButtonEventArgs e) {
			this.LeftMouseDownAt = e.GetPosition(null);
			Mouse.Capture(this);
		}

		private void OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
			Mouse.Capture(null);
		}

		private void OnPreviewMouseMove(object sender, MouseEventArgs e) {
			if (e.LeftButton == MouseButtonState.Pressed && Mouse.Captured == this) {
				Point MousePos = e.GetPosition(null);
				Vector Diff = this.LeftMouseDownAt - MousePos;

				if (Math.Abs(Diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(Diff.Y) > SystemParameters.MinimumVerticalDragDistance) {
					Mouse.Capture(null);
//					DataObject DragData = new DataObject("Item", this.Item);
//					DragDrop.DoDragDrop(this, DragData, DragDropEffects.Move);
				}
			}
		}

		private void OnDrop(object sender, DragEventArgs e) {
			if (this.DropState == DropStates.Accept) {
//				if (e.Data.GetDataPresent("Item"))
//					this.Item.Add(e.Data.GetData("Item") as ItemViewModel);
			}
			this.DropState = DropStates.None;
		}

		private void OnDragEnter(object sender, DragEventArgs e) {
//			ItemViewModel i = (e.Data.GetData("Item") as ItemViewModel);

//			if (i == null || i == this.Item) {
//				e.Effects = DragDropEffects.None;
//				this.DropState = DropStates.Deny;
//			} else {
//				e.Effects = DragDropEffects.Move;
//				this.DropState = DropStates.Accept;
//			}
			e.Handled = true;
		}

		private void OnDragLeave(object sender, DragEventArgs e) {
			this.DropState = DropStates.None;
		}

		private void OnDelete(object sender, ExecutedRoutedEventArgs e) {
//			this.Item.Delete();
		}
	}
}